package com.big_event.controller;

import com.big_event.pojo.Article;
import com.big_event.pojo.PageBean;
import com.big_event.pojo.Result;
import com.big_event.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    //1.新增文章
    @PostMapping
    public Result add(@RequestBody @Validated Article article){
        articleService.add(article);
        return Result.success();
    }

    //2.文章列表（条件分类）
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Article> pb =  articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }

    //3.获取文章详情
    @GetMapping("/getA")
    public Result<Article> getA(Integer id){
        Article ar=articleService.findById(id);
        return Result.success(ar);
    }

    //4.更新文章
    @PutMapping
    public Result updateA(@RequestBody @Validated Article article){
        articleService.updateA(article);
        return Result.success();
    }

    //5.删除文章
    @DeleteMapping
    public Result delete(Integer id){
        articleService.delete(id);
        return Result.success();
    }

}
